#!/usr/bin/env bash
#
# Test zone management operations.
#

seq="$(basename $0)"
echo "QA output created by $seq"
status=1 # failure is the default!

_cleanup()
{
  _cleanup_test_img
  sudo -n rmmod null_blk
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ../common.rc
. ../common.filter
. ../common.qemu

# This test only runs on Linux hosts with raw image files.
_supported_fmt raw
_supported_proto file
_supported_os Linux

sudo -n true || \
    _notrun 'Password-less sudo required'

IMG="--image-opts -n driver=host_device,filename=/dev/nullb0"
QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT

echo "Testing a null_blk device:"
echo "case 1: if the operations work"
sudo -n modprobe null_blk nr_devices=1 zoned=1
sudo -n chmod 0666 /dev/nullb0

echo "(1) report the first zone:"
$QEMU_IO $IMG -c "zrp 0 1"
echo
echo "report the first 10 zones"
$QEMU_IO $IMG -c "zrp 0 10"
echo
echo "report the last zone:"
$QEMU_IO $IMG -c "zrp 0x3e70000000 2" # 0x3e70000000 / 512 = 0x1f380000
echo
echo
echo "(2) opening the first zone"
$QEMU_IO $IMG -c "zo 0 268435456"  # 268435456 / 512 = 524288
echo "report after:"
$QEMU_IO $IMG -c "zrp 0 1"
echo
echo "opening the second zone"
$QEMU_IO $IMG -c "zo 268435456 268435456" #
echo "report after:"
$QEMU_IO $IMG -c "zrp 268435456 1"
echo
echo "opening the last zone"
$QEMU_IO $IMG -c "zo 0x3e70000000 268435456"
echo "report after:"
$QEMU_IO $IMG -c "zrp 0x3e70000000 2"
echo
echo
echo "(3) closing the first zone"
$QEMU_IO $IMG -c "zc 0 268435456"
echo "report after:"
$QEMU_IO $IMG -c "zrp 0 1"
echo
echo "closing the last zone"
$QEMU_IO $IMG -c "zc 0x3e70000000 268435456"
echo "report after:"
$QEMU_IO $IMG -c "zrp 0x3e70000000 2"
echo
echo
echo "(4) finishing the second zone"
$QEMU_IO $IMG -c "zf 268435456 268435456"
echo "After finishing a zone:"
$QEMU_IO $IMG -c "zrp 268435456 1"
echo
echo
echo "(5) resetting the second zone"
$QEMU_IO $IMG -c "zrs 268435456 268435456"
echo "After resetting a zone:"
$QEMU_IO $IMG -c "zrp 268435456 1"
echo
echo
echo "(6) append write" # the physical block size of the device is 4096
$QEMU_IO $IMG -c "zrp 0 1"
$QEMU_IO $IMG -c "zap -p 0 0x1000 0x2000"
echo "After appending the first zone firstly:"
$QEMU_IO $IMG -c "zrp 0 1"
$QEMU_IO $IMG -c "zap -p 0 0x1000 0x2000"
echo "After appending the first zone secondly:"
$QEMU_IO $IMG -c "zrp 0 1"
$QEMU_IO $IMG -c "zap -p 268435456 0x1000 0x2000"
echo "After appending the second zone firstly:"
$QEMU_IO $IMG -c "zrp 268435456 1"
$QEMU_IO $IMG -c "zap -p 268435456 0x1000 0x2000"
echo "After appending the second zone secondly:"
$QEMU_IO $IMG -c "zrp 268435456 1"

# success, all done
echo "*** done"
rm -f $seq.full
status=0
